package weaver.interfaces.zhlx.duss.action.salesmodule.crm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.CrmUtil;
import weaver.interfaces.zhlx.duss.utils.PropUltraUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年12月23日 09:02
 */
public class WeaCpggpsdAction extends BaseBean implements Action {
    private static final String RES_CODE_ZERO = "0";
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        String requestname = requestInfo.getRequestManager().getRequestname();
        writeLog(">>>>>WeaCpggpsdAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            String message = "调用CRM系统接口返回:";
            CrmUtil crmUtil = new CrmUtil();
            rs.executeQuery("select sqrgh,sqlx,sqrq,scd,gdrq,sqdbh,wwkhbm,khwlms,jldlh," +
                    "glsjid,khlhdm,cpzl,cpyy,cpdj,cc,cz,jx,kzfs,xh,hd,dzllx,dzsx,dzxx,sfyldlhwb,"+
                    "epiddxh,epicpgg,fj,bz,wbwlz,cdcpyywbwlz,cpkfdj from " + tablename + " where requestid=? and sfxyglsj=0",requestid);
            if(rs.next()) {
                String sqrgh = Util.null2String(rs.getString("sqrgh"));//申请人工号
                String sqlx = Util.null2String(rs.getString("sqlx"));//申请类型
                String sqrq = Util.null2String(rs.getString("sqrq"));//申请日期
                String scd = Util.null2String(rs.getString("scd"));//生产地
                String gdrq = Util.null2String(rs.getString("gdrq"));//归档日期
                String sqdbh = Util.null2String(rs.getString("sqdbh"));//申请单编号
                String wwkhbm = Util.null2String(rs.getString("wwkhbm"));//五位客户编码
                String khwlms = Util.null2String(rs.getString("khwlms"));//客户物料描述
                String glsjid = Util.null2String(rs.getString("glsjid"));//关联商机
                String khlhdm = Util.null2String(rs.getString("khlhdm"));//客户料号编码
                String jldlh = Util.null2String(rs.getString("jldlh"));//临时料号
                String cpzl = Util.null2String(rs.getString("cpzl"));//中环加工产品类型
                String cpyy = Util.null2String(rs.getString("cpyy"));//产品应用
                String cpdj = Util.null2String(rs.getString("cpdj"));//产品等级
                String cc = Util.null2String(rs.getString("cc"));//尺寸
                String cz = Util.null2String(rs.getString("cz"));//掺质
                String jx = Util.null2String(rs.getString("jx"));//晶向
                String kzfs = Util.null2String(rs.getString("kzfs"));//生长方式
                String xh = Util.null2String(rs.getString("xh"));//型号
                String hd = Util.null2String(rs.getString("hd"));//厚度(um)
                String dzllx = Util.null2String(rs.getString("dzllx"));//电阻率类型
                String dzsx = Util.null2String(rs.getString("dzsx"));//电阻上限(Ω)
                String dzxx = Util.null2String(rs.getString("dzxx"));//电阻下限(Ω)
                String sfyldlhwb = Util.null2String(rs.getString("sfyldlhwb"));//是否有连档料号
                String epiddxh = Util.null2String(rs.getString("epiddxh"));//EPI导电型号
                String epicpgg = Util.null2String(rs.getString("epicpgg"));//EPI产品规格
                String fj = Util.null2String(rs.getString("fj"));//附件
                String bz = Util.null2String(rs.getString("bz"));//备注
                String wbwlz = Util.null2String(rs.getString("wbwlz"));//产品应用（外部物料组）
                String cdcpyywbwlz = Util.null2String(rs.getString("cdcpyywbwlz"));//衬底产品应用（外部物料组）
                String cpkfdj = Util.null2String(rs.getString("cpkfdj"));//产品开发等级

                JSONObject dataJson = new JSONObject();
                JSONObject object_data = new JSONObject();
                object_data.put("dataObjectApiName", "spec_review_form__c");
                object_data.put("title__c", requestname);
                object_data.put("owner_jobNo__c", sqrgh);
                object_data.put("application_type__c", sqlx);
                sqrq = !"".equals(sqrq) ? sqrq + " 00:00:00" : sqrq;
                object_data.put("field_QjMmd__c", crmUtil.dateToStamp(sqrq));
                object_data.put("production_location__c", scd.split(","));
                object_data.put("process_start_time__c", crmUtil.dateToStamp(sqrq));
                gdrq = !"".equals(gdrq) ? gdrq + " 00:00:00" : gdrq;
                object_data.put("process_end_time__c", crmUtil.dateToStamp(gdrq));
                object_data.put("ec_approvalStatus__c", "3");
                object_data.put("ec_review_single_number__c", sqdbh);
                object_data.put("account_no__c", wwkhbm);
                object_data.put("temp_part_number__c", jldlh);
                object_data.put("customer_material_descript__c", khwlms);
                object_data.put("business_opportunity__c", glsjid);
                object_data.put("customer_part_code__c", khlhdm);
                object_data.put("processed_product_type__c", cpzl);
                object_data.put("product_application__c", cpyy);
                object_data.put("product_level__c", cpdj);
                object_data.put("size__c", cc);
                object_data.put("blend_material__c", cz);
                object_data.put("crystal_orientation__c", jx);
                object_data.put("growth_method__c", kzfs);
                object_data.put("model__c", xh);
                object_data.put("thickness__c", hd);
                object_data.put("resistance_type__c", dzllx);
                object_data.put("resistance_upper_limit__c", dzsx);
                object_data.put("resistance_lower_limit__c", dzxx);
                object_data.put("is_continuous_style__c", sfyldlhwb);
                object_data.put("epi_conductive__c", epiddxh);
                object_data.put("epi_specification__c", epicpgg);
                JSONArray szhtList = crmUtil.getImagefile(fj);
                object_data.put("attachment__c", szhtList);
                object_data.put("remark__c", bz);
                object_data.put("product_application_external__c", wbwlz);
                object_data.put("substrate_product_applicat__c", cdcpyywbwlz);
                object_data.put("product_development_level__c", cpkfdj);
                JSONObject propVal = PropUltraUtil.getPropMainVal("zhlxcrm");
                String username = propVal.getString("username");
                object_data.put("is_deleted", false);
                object_data.put("owner", username.split(","));
                object_data.put("record_type", "default__c");
				dataJson.put("skipDataStatusValidate", true);
                dataJson.put("object_data", object_data);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaCpggpsdAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaCpggpsdAction>>>>>dataJson=" + dataJson);
                JSONObject resultJson = crmUtil.excuteHandler(dataJson, dataMap, "WeaCpggpsdAction");
                writeLog(">>>>>WeaCpggpsdAction>>>>>resultJson=" + resultJson);
                String resultMessage = Util.null2String(resultJson.getString("message"));
                if (!"success".equals(resultMessage)) {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                } else {
                    JSONObject resultJsonObj = resultJson.getJSONObject("data");
                    String errorCode = resultJsonObj.getString("errorCode");
                    if (!RES_CODE_ZERO.equals(errorCode)) {
                        message += Util.null2String(resultJsonObj.getString("errorMessage"));
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaCpggpsdAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}
